<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\CreateCaseRequest;
use App\Http\Requests\UpdateCaseRequest;
use App\Cases;
use Illuminate\Support\Facades\Storage;
use Image;
use MyFlash;
class CaseController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $case=Cases::orderBy('id','desc')->paginate(15);
        return view('admin.case.index',compact('case'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.case.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(CreateCaseRequest $request)
    {
        $case=new Cases;
        $case->title=$request->title;
        $case->remark=$request->remark;
        $case->sort=$request->sort;
        $case->pic=$this->thumbil($request);
        if($case->save()){
            MyFlash::success('案例添加成功');
            return redirect()->route('admin.case.index');
        }

    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $case=Cases::findOrFail($id);
        return view('admin.case.edit',compact('case'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(UpdateCaseRequest $request, $id)
    {
        $case=Cases::findOrFail($id);
        $case->title=$request->title;
        $case->remark=$request->remark;
        $case->sort=$request->sort;
        if($request->file('pic')){
            $this->exitsimg($case->pic);
            $case->pic=$this->thumbil($request);

        }

        if($case->save()){
            MyFlash::success('案例添加成功');
            return redirect()->route('admin.case.index');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $case=Cases::findOrFail($id);
        $blur=Cases::where('id',$case->id)->delete();
        $this->exitsimg($case->pic);
        if($blur){
            MyFlash::success('案例删除成功');
            return redirect()->route('admin.case.index');
        }
    }
    /**
     * 图片上传处理
     */
    public function thumbil($request){
        if ($request->file('pic')) {
            $thumb=$request->pic;
            $name=$thumb->hashName();
            $thumb->storeAs('/public/uploads/',$name);
            $path=storage_path('app/public/uploads/'.$name);
            // Image::make($thumb)->resize(370,260)->save($path);
            return $name;
        }

    }
    // 检查文件是否存在
    public function exitsimg($img){
        $path='public/uploads/'.$img;
        $exists = Storage::disk('local')->exists($path);
        if ($exists) {
            $this->delimg($path);
        }
    }
    // 删除图片
    public function delimg($path){
        $result=Storage::delete($path);
        if ($result) {
            return true;
        }else {
            return false;
        }
    }
    //批量删除
    public function delh(Request $request){
        $ids=$request->post()['ids'];
        foreach($ids as $item){
            $this->destroy($item);
        }
        MyFlash::success('案例删除成功');
        return redirect()->route('admin.case.index');
    }
}
